/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getTableList,
  getDownloadList,
  rePostage,
  deleteList,
  editList,
  createBill,
  getSubDaksList,
} from "@/services/orderCalculate";
import { message } from "antd";
import { isLegalData } from "@/utils/utils";

export default {
  namespace: "order_calculate",
  state: {},

  effects: {
    *getTableList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getTableList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve({
          total: data?.total,
          list: isLegalData(data.list, []).map(item => ({ ...item, id: item.waybill_no })),
        });
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getDownloadList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getDownloadList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *rePostage({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(rePostage, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },

    *deleteList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(deleteList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *editList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *createBill({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(createBill, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0) {
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getSubDaksList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getSubDaksList, payload);
      if (!response) return;
      const { code, data } = response;
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            yzList: isLegalData(data, []),
          },
        });
        __dva_resolve(isLegalData(data, []));
      } else {
        __dva_reject(response);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
